﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using Verse;

namespace ARMIR
{
    public class HediffComp_mindCrush : HediffComp {
        public HediffCompProperties_mindCrush Props => (HediffCompProperties_mindCrush)props;
        private int gapMindCheck;
        private int gapCrush;
        public override void CompPostMake()
        {
            base.CompPostMake();
            gapMindCheck = 0;
            gapCrush = 0;
        }
        public override void CompExposeData()
        {
            base.CompExposeData();
            Scribe_Values.Look(ref gapCrush, "思维崩毁冷却",0);
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);
            gapMindCheck++;
            if (gapMindCheck > 100) {
                gapMindCheck = 0;
                checkMindState();
            }
            if (gapCrush > 0) {
                gapCrush--;
            }
        }
        private void checkMindState() {
            if (gapCrush == 0) {
                if (parent.pawn.MentalState != null) {
                    Hediff down = HediffMaker.MakeHediff(DefDatabase<HediffDef>.GetNamed("ARMIR_mindCrush"), parent.pawn);
                    parent.pawn.health.AddHediff(down);
                    ARMIR_language.isThatChinese(out bool yesItIs);
                    if (yesItIs)
                    {
                        string title = "思维崩毁";
                        string body = "迷迭香因为巨大的精神负担陷入了思维崩毁状态！\n" +
                            "迷迭香失去了对现实世界的感知以及其能力的把握\n" +
                            "现在她失控的力量会将其周围的一切都摧毁！";
                        Find.LetterStack.ReceiveLetter(
                            title.Translate(), // 标题
                            body.Translate(), // 正文
                            LetterDefOf.ThreatBig, // 信件定义
                            parent.pawn, // 可选的点击目标
                            parent.pawn.Faction // 可选的关联阵营（影响信件颜色等）
                        );
                    }
                    else {
                        string title = "Mind Crush";
                        string body = "Rosmontis fell into a Mind Crush due to immense psychological stress!\n" +
                            "Rosmontis lost her sense of the real world and control over her abilities.\n" +
                            "Now her uncontrollable power will destroy everything around her!";
                        Find.LetterStack.ReceiveLetter(
                            title.Translate(), // 标题
                            body.Translate(), // 正文
                            LetterDefOf.ThreatBig, // 信件定义
                            parent.pawn, // 可选的点击目标
                            parent.pawn.Faction // 可选的关联阵营（影响信件颜色等）
                        );
                    }
                    gapCrush = 60000;
                }
            }
            //Log.Message("[DEBUG] 角色状态" + parent.pawn.MentalState);
            //Log.Message("[DEBUG] 角色状态" + parent.pawn.InMentalState);
        }
    }
}
